<?php
/*
Plugin Name: Smiley Javascript Buttons
Plugin URI: http://aranea.zuavra.net/index.php/18/
Description: Lets you display a row of clickable smilies next to your comment textarea. Thanks to Priyadi Iman Nurcahyo for the inspiration.
Version: 1.5
Author: Skippy, skippy zuavra net
Author URI: http://aranea.zuavra.net/
License:

Copyright (c)2005-2007 Zuavra.net

Permission is hereby granted, free of charge, to any person obtaining a copy of 
this software and associated documentation files (the "Software"), to deal in 
the Software without restriction, including without limitation the rights to 
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 
the Software, and to permit persons to whom the Software is furnished to do so, 
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all 
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

global $SJB_IMAGE_URL; $SJB_IMAGE_URL='';
global $SJB_SMILIES; $SJB_SMILIES=array();

function SJB_findImageURL() {
    $client_root = get_settings('siteurl');
    $server_root = str_replace("index.php","",$_SERVER['SCRIPT_FILENAME']);
    // define possible locations for the smilies
    $locations = array(
        // WP 2.x+
        '/wp-includes/images/smilies/',
        // WP 1.5.x
        '/wp-images/smilies/',
    );
    // find a good location
    foreach ($locations as $dir) {
        if (is_dir($server_root.'/'.$dir)) return $client_root.'/'.$dir;
    }
    // nothing found, for some reason;
    // output the entry for the latest WP and hope for the best
    return $client_root.'/'.$locations[0];
}

function SJB_getSmilies() {
    static $got_smilies;
    if ($got_smilies) return;
    global $wpsmiliestrans;
    global $SJB_IMAGE_URL, $SJB_SMILIES;

    $SJB_IMAGE_URL = SJB_findImageURL();
    if (!is_array($wpsmiliestrans)) smilies_init();
    $SJB_SMILIES = array_map('trim',array_flip($wpsmiliestrans));
    $got_smilies = true;
}

function SJB_outputSmilies($textarea_name='comment') {
    global $SJB_IMAGE_URL, $SJB_SMILIES;
    SJB_getSmilies();

    // output JS functions
    ?><script type="text/javascript"><!--
    function SJB_appendSmiley(text) {
        var comment=document.getElementById('<?=$textarea_name?>');
        comment.value=comment.value+' '+text;
    }
    //--></script><?php

    // output clickable smilies
    echo '<script type="text/javascript"><!--'."\n";
    foreach($SJB_SMILIES as $img => $code) {
        echo "document.write('<a href=\"javascript:SJB_appendSmiley(\\'".addslashes($code)."\\')\">');\n";
        echo "document.write('<img src=\"".$SJB_IMAGE_URL.$img."\" alt=\"".$code."\" class=\"wp-smiley\" title=\"".$code."\" />');\n";
        echo "document.write('</a> ');\n";
    }
    echo "//--></script>\n";
    echo '<noscript>';
    echo 'You could see clickable smiley faces here if you had JavaScript activated.';
    echo '</noscript>';
}

function SJB_adminSmilies() {
    global $SJB_SMILIES;
    if (!is_array($SJB_SMILIES)) SJB_getSmilies();

    $OUTPUT="<script type=\"text/Javascript\"><!--\n";
    $OUTPUT.="if(typeof(edButtons)!='undefined'){\n";
    $i=0;
    foreach($SJB_SMILIES as $img => $code) {
        $OUTPUT.="edButtons[edButtons.length]=new edButton('ed_smilie_'+".$i.",'".addslashes($code)."',' ".addslashes($code)."','','',-1);\n";
        $i++;
    }
    $OUTPUT.="}\n";
    $OUTPUT.='//--></script>';
    return $OUTPUT;
}

?>